﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace DisposeText
{

    public enum WordFeature
    {
        Positive,
        Negative,
        Ambiguous,//歧义词-既有褒义又有贬义
        Opposite,//反义词
        Stress,//强调词
        Unknown//未知词
    } 

    class WordBook
    {
        private Hashtable wordBook;

        public WordBook(string path)
        {
            wordBook = new Hashtable();
             LoadData(path + "/Sentiment_Data/");
        }

        /// <summary>
        /// 通过单词获得单词的特征
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public WordFeature GetFeatureByWord(string word)
        {
            if (wordBook.ContainsKey(word))
            {
                return (WordFeature)wordBook[word];
            }
            else
            {
                return WordFeature.Unknown;
            }
        }

        /// <summary>
        /// 获得一个特征的所有单词
        /// </summary>
        /// <param name="wordFeature"></param>
        /// <returns></returns>
        public List<string> GetWordsByFeature(WordFeature wordFeature)
        {
            List<string> wordList = new List<string>();
            foreach (DictionaryEntry wordStruct in wordBook)
            {
                if ((WordFeature)wordStruct.Value == wordFeature)
                {
                    wordList.Add((string)wordStruct.Key);
                }
                else if ((wordFeature == WordFeature.Positive || wordFeature == WordFeature.Negative) && (WordFeature)wordStruct.Value == WordFeature.Ambiguous)
                {
                    wordList.Add((string)wordStruct.Key);
                }
            }
            return wordList;
        }

        private void ReadFileToHashtable(string path, WordFeature wordFeature)
        {
            using (StreamReader sr = new StreamReader(path, Encoding.Default))
            {
                while (sr.Peek() != -1)
                {
                    string word = sr.ReadLine().Trim();

                    if (!wordBook.ContainsKey(word))
                    {
                        wordBook.Add(word, wordFeature);
                    }
                    else if (wordFeature == WordFeature.Negative || wordFeature == WordFeature.Positive)
                    {
                        if ((WordFeature)wordBook[word] != wordFeature)
                        {
                            wordBook[word] = WordFeature.Ambiguous;
                        }
                    }
                }//end of while
            }//end of using
        }

        private void LoadData(string path)
        {
            ReadFileToHashtable(path + "正面评价词语（中文）.txt", WordFeature.Positive);
            ReadFileToHashtable(path + "正面情感词语（中文）.txt", WordFeature.Positive);
            ReadFileToHashtable(path + "负面评价词语（中文）.txt", WordFeature.Negative);
            ReadFileToHashtable(path + "负面情感词语（中文）.txt", WordFeature.Negative);
            ReadFileToHashtable(path + "否定词语（中文）.txt", WordFeature.Opposite);
        }

        
    }
}
